import 'package:get/get.dart';
import 'package:smart_home/model/device.dart';
import 'package:smart_home/service/network/api_search_device.dart';

class SearchDeviceController extends GetxController {
  RxString deviceName = "".obs;
  RxList records = [].obs;
  RxInt currentPage = 1.obs;
  RxInt pageSize = 15.obs;
  RxInt pages = 0.obs;
  RxString loadText = "".obs;
  changeDeviceName(String pDeviceName) {
    print("$pDeviceName===========devicename========");
    deviceName.value = pDeviceName;
  }

  searchChangeListData() {
    SerarchDeviceApi.getDeviceList(
            deviceName: deviceName.value, limit: pageSize.value, cursor: 1)
        .then((value) => {
              print("执行=============$deviceName"),
              pages.value = 0,
              records.value = Device.fromJson(value).result!.records!.toList()
            });
  }

  dropDownChangeListData() {
    if (currentPage.value < pages.value) {
      currentPage.value++;
      loadText.value = "正在拼命加载中...${records.length}";
    }
    print("$currentPage======${pages.value}");
    if (currentPage.value == pages.value) {
      loadText.value = "没有更多数据了";
      return null;
    }
    SerarchDeviceApi.getDeviceList(
            deviceName: deviceName.value,
            limit: pageSize.value,
            cursor: currentPage.value)
        .then((value) => {
              print("执行========$pages===============$currentPage"),
              pages.value = Device.fromJson(value).result!.pages!,
              records.addAll(Device.fromJson(value).result!.records!.toList())
            });
  }
}
